using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;

public class TwistRollSceneMgr : BaseScene
{
    SCamerContrller camerContrller;

    Light light;
    // Start is called before the first frame update
    void Start()
    {
        AssetBundleManager abmgr = AssetBundleManager.Instance;

        light = GameObject.FindObjectOfType<Light>();

        light.intensity = 0.5f;

        ObjectPoolManager.Instance.InitBundles(abmgr.bundles[abmgr.POOL_BUNDLE], abmgr.bundles);

        Destroy(Camera.main.gameObject);

        SCamerContrller camerContrller = ObjectPoolManager.Instance.DePool<SCamerContrller>("SCamerContrller", true, "CommonPre");

        InitClass(3 , 3,1);

        InitMouseQueue();

        DOTween.Init();
    }

    DiceBehaviour[,] dics;
    void InitClass(int x, int y, int c) {
        dics = new DiceBehaviour[x, y];
        Vector3 o = new Vector3((x-1) * 0.5f, 0, (y-1) * 0.5f);
        Debug.Log(o);
        DiceBehaviour db;
        for (int i = 0; i < x; i++)
        {
            for (int j = 0; j < y; j++)
            {
                db = ObjectPoolManager.Instance.DePool<DiceBehaviour>("Dice");
                db.transform.position = (new Vector3(i, 0, j)-o);
                db.SetID(i, j);
                dics[i, j] = db;
            }
        }
    }

    void InitMouseQueue() {
        mouseQueue = new Queue<Vector2>();
    }

    RaycastHit hit;
    Queue<Vector2> mouseQueue;
    DiceBehaviour selectDB;
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            mouseQueue.Clear();
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            {
                selectDB = hit.collider.gameObject.GetComponent<DiceBehaviour>();
            }
        }

        if (Input.GetMouseButton(0) && selectDB != null)
        {

            for (int i = 0; i < dics.GetLength(0); i++)
            {
                dics[i, selectDB.Y].StartShake();
            }
            for (int i = 0; i < dics.GetLength(1); i++)
            {
                dics[selectDB.X, i].StartShake();
            }
            mouseQueue.Enqueue(Input.mousePosition);
        }

        if (Input.GetMouseButtonUp(0) && mouseQueue.Count>0)
        {
            Vector2 startP = mouseQueue.Peek();
            Vector2 endP=Vector2.zero;
            foreach (var item in mouseQueue)
            {
                endP = item;
            }
            selectDB = null;
            Debug.Log(startP - endP);
        }
    }


}
